package baseproject.com.mybaseproject.net

import com.bybops.app.MyApplication
import com.bybops.model.api.URLConstant
import com.hazz.kotlinmvp.api.ApiService
import com.hazz.kotlinmvp.api.ConfigService
import com.hazz.kotlinmvp.api.FileService
import com.zhy.http.okhttp.https.HttpsUtils
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okio.Buffer
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.io.InputStream
import java.util.concurrent.TimeUnit


/**
 * Created by xuhao on 2017/11/16.
 *
 */

object RetrofitManager{
    //config实例
    val configService: ConfigService by lazy (LazyThreadSafetyMode.SYNCHRONIZED){
        getConfigRetrofit().create(ConfigService::class.java)
    }
    //api实例
    val apiService: ApiService by lazy (LazyThreadSafetyMode.SYNCHRONIZED){
        getApiRetrofit().create(ApiService::class.java)
    }
    //文件api实例
    val fileService: FileService by lazy (LazyThreadSafetyMode.SYNCHRONIZED){
        getFileRetrofit().create(FileService::class.java)
    }

    private fun getConfigRetrofit(): Retrofit {
        // 获取retrofit的实例
        return Retrofit.Builder()
                .baseUrl(URLConstant.mConfigUrl)  //自己配置
                .client(getOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

    }
    private fun getApiRetrofit(): Retrofit {
        // 获取retrofit的实例
        return Retrofit.Builder()
                .baseUrl(URLConstant.mApiUrl)  //自己配置
                .client(getOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

    }
    private fun getFileRetrofit(): Retrofit {
        // 获取retrofit的实例
        return Retrofit.Builder()
                .baseUrl(URLConstant.mFileUrl)  //自己配置
                .client(getOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

    }
    private fun getOkHttpClient(): OkHttpClient {
        //添加一个log拦截器,打印所有的log
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        //可以设置请求过滤的水平,body,basic,headers
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

        //设置 请求的缓存的大小跟位置
        val cacheFile = File(MyApplication.instance?.cacheDir, "cache")
        val cache = Cache(cacheFile, 1024 * 1024 * 50) //50Mb 缓存的大小

        val sSLParams = HttpsUtils.getSslSocketFactory(arrayOf(Buffer().writeUtf8(cer_naton_new).inputStream()), null as InputStream?, null as String?)

        return OkHttpClient.Builder()
//              .addInterceptor(addCacheInterceptor())
                .addInterceptor(httpLoggingInterceptor) //日志,所有的请求响应度看到
                .cache(cache)  //添加缓存
                .connectTimeout(60L, TimeUnit.SECONDS)
                .readTimeout(60L, TimeUnit.SECONDS)
                .writeTimeout(60L, TimeUnit.SECONDS)
//                .sslSocketFactory(sSLParams.sSLSocketFactory , sSLParams.trustManager)
//                .hostnameVerifier { hostname, sslSession ->
//                    val hv = HttpsURLConnection.getDefaultHostnameVerifier()
//                    hv.verify("*.naton.cn", sslSession)
//
//                }
                .build()
    }

    val cer_naton_new = "-----BEGIN CERTIFICATE-----\n" +
            "MIIGATCCBOmgAwIBAgIQArWkpsbqu5d0M6rrtpHpBzANBgkqhkiG9w0BAQsFADBN\n" +
            "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5E\n" +
            "aWdpQ2VydCBTSEEyIFNlY3VyZSBTZXJ2ZXIgQ0EwHhcNMTgwMzIzMDAwMDAwWhcN\n" +
            "MTgxMjEyMTIwMDAwWjB3MQswCQYDVQQGEwJDTjEPMA0GA1UEBwwG5YyX5LqsMTAw\n" +
            "LgYDVQQKDCfljJfkuqzlqIHogZTlvrfpqqjnp5HmioDmnK/mnInpmZDlhazlj7gx\n" +
            "EDAOBgNVBAsTB0lUIERlcHQxEzARBgNVBAMMCioubmF0b24uY24wggEiMA0GCSqG\n" +
            "SIb3DQEBAQUAA4IBDwAwggEKAoIBAQC4wxxCwwZGsutzHZP9UaW9D4k1eRl7ELF0\n" +
            "95QMPqFkbNAegBjluaGhwRfI0ouO0pwkTHIO70sSf9PcBruTt6kSj0Nv60C7sbTR\n" +
            "IAr5ZtogYeG6aPLMPMYd5QquwdmIB0beLqSVZaknu6Eac7cZYiw1tKDQbfn6b3GM\n" +
            "rGIreICkLwdNZk8OF2ud+pwB8Y8dFJR1YzCM4ebvmxA6OlTPSdejpjz1FB28tF/3\n" +
            "Y87MJQcYv/HtplUnSiTyQpy9JJpOrpy72hLaQATDO5TzfqfAKVhbXUbykjd1mzEQ\n" +
            "7z4fh09ctpViBJb97kRLV7Jvph2B1jNLewpPpbL+x74pnigrxwBzAgMBAAGjggKx\n" +
            "MIICrTAfBgNVHSMEGDAWgBQPgGEcgjFh1S8o541GOLQs4cbZ4jAdBgNVHQ4EFgQU\n" +
            "2gGYGIfKh1lWYMHOJTqaUkdM1bcwHwYDVR0RBBgwFoIKKi5uYXRvbi5jboIIbmF0\n" +
            "b24uY24wDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEF\n" +
            "BQcDAjA6BgNVHR8EMzAxMC+gLaArhilodHRwOi8vY2RwMS5kaWdpY2VydC5jb20v\n" +
            "c3NjYS1zaGEyLWc2LmNybDBMBgNVHSAERTBDMDcGCWCGSAGG/WwBATAqMCgGCCsG\n" +
            "AQUFBwIBFhxodHRwczovL3d3dy5kaWdpY2VydC5jb20vQ1BTMAgGBmeBDAECAjB9\n" +
            "BggrBgEFBQcBAQRxMG8wJQYIKwYBBQUHMAGGGWh0dHA6Ly9vY3NwMS5kaWdpY2Vy\n" +
            "dC5jb20wRgYIKwYBBQUHMAKGOmh0dHA6Ly9jYWNlcnRzLmRpZ2ljZXJ0LmNvbS9E\n" +
            "aWdpQ2VydFNIQTJTZWN1cmVTZXJ2ZXJDQS5jcnQwCQYDVR0TBAIwADCCAQUGCisG\n" +
            "AQQB1nkCBAIEgfYEgfMA8QB2AKS5CZC0GFgUh7sTosxncAo8NZgE+RvfuON3zQ7I\n" +
            "DdwQAAABYlDpODgAAAQDAEcwRQIhAMG7CwT6cVJ8s4PvHqJNdxAU6F202rDucsCw\n" +
            "jyLO49YpAiBVE9AHU1h7617oBjV202c0uYuwBp/9hHwmyqv8fABuEAB3AG9Tdqwx\n" +
            "8DEZ2JkApFEV/3cVHBHZAsEAKQaNsgiaN9kTAAABYlDpOPoAAAQDAEgwRgIhANuE\n" +
            "rq2RwfkOZ7D28rom+ejxH4nE/ksenGYmPRYAmBWQAiEAr5IkAyoaAbfCS8MoVc6D\n" +
            "+eG6KlYcdjrh1djFAsyr4+wwDQYJKoZIhvcNAQELBQADggEBAGrzK/XZtxvO/2xl\n" +
            "uO8PbL7t3VtK95wt22qIa+zsZ8e+zdOElZ/IIXpIuJ21pRDMW34KeQeEk21CGgUS\n" +
            "abjNQp3+uYattU6XNJyGQ02kFH1Wtdvh1/BOU/pSimBhDO8Op7Y46LrlCr4a+qyc\n" +
            "02miMZmGNPEWihzKULcagz0SCNgRXbUb9WCpA7tDCbYLiNtKv0IRCRP8haD6ohKK\n" +
            "RaxVvoJu7sKs+NrdHhzWWOxL3EQbNCfDcg1tcXZ4ozlZbMDhHtC7OSXpvZQoIIaU\n" +
            "1HeEQDknJzNMCX4o7N5daFyURNhV2v3WTYfy0FFyhZgMZaZwtAaJbvQQguhg1me4\n" +
            "KeghcUM=\n" +
            "-----END CERTIFICATE-----"
}
